(function() {
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    var appid = GetQueryString("appid");
    var name = Base64.decode(GetQueryString("name"));
    var phone = GetQueryString("phone");
    var oldphone = GetQueryString("phone");
    $('#name').attr("value", name);
    $('#phone').attr("value", phone);

    var headers = {
            'appid': appid,
            'token': '',
            'version': 'v1', //版本号
            'fromsource': 5, //来源 
            'device': 'wechat', //设备
            'content-type': 'application/x-www-form-urlencoded'
        }
        // 测试
        // var request = 'https://n.qikeya.com/erp_api/public/index.php/';
        // 正式
    var request = 'https://api-erp.qikeya.cn/';


    // 发送验证码
    $('.sendtime').html('发送验证码');

    // 点击发送验证码
    $('.sendtime').click(function() {
        var tel = $('#phone').val();
        if ($('.sendtime').hasClass('sendtimeactive')) {

        } else {
            if (tel == '') {
                layer.open({
                    content: '请输入手机号',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(tel))) {
                layer.open({
                    content: '请输入正确的手机号',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else {
                $('.sendtime').addClass('sendtimeactive');
                var currentTime = 60;
                $('.sendtime').html(currentTime + 's重新发送');
                var time = setInterval(function() {
                    currentTime--;
                    $('.sendtime').html(currentTime + 's重新发送');
                    if (currentTime <= 0) {
                        clearInterval(time);
                        $('.sendtime').html('发送验证码');
                        $('.sendtime').removeClass('sendtimeactive')
                    }
                }, 1000)
                $.ajax({
                    url: request + 'crmapi/User/smsCode',
                    data: {
                        tel: tel,
                        from: 2
                    },
                    headers: headers,
                    dataType: "json",
                    success: function(res) {
                        if (Number(res.errcode) == 0) {
                            layer.open({
                                content: '发送成功',
                                skin: 'msg',
                                time: 1 //1秒后自动关闭
                            });

                        } else {

                            layer.open({
                                content: res.msg,
                                skin: 'msg',
                                time: 1 //1秒后自动关闭
                            });
                        }
                    }
                })
            }
        }
    })
    $('.submitbut').click(function() {
        var tel = $('#phone').val();
        var nae = $('#name').val();
        var message = $('#code').val();
        var name = nae.replace(/\s+/g, "");
        if ($(this).hasClass('submitbutactive')) {

        } else {
            if (name == '') {
                layer.open({
                    content: '请输入姓名',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else if (!(/^[\u2E80-\u9FFF]+$/.test(name))) {
                layer.open({
                    content: '姓名只能为汉字',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else if (Number(name.length) > 5) {
                layer.open({
                    content: '姓名最多为5个字',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else if (tel == '') {
                layer.open({
                    content: '请输入手机号',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(tel))) {
                layer.open({
                    content: '请输入正确的手机号',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else if (message == '') {
                layer.open({
                    content: '请输入验证码',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else {
                $(this).addClass('submitbutactive');
                $.ajax({
                    url: request + 'crmapi/User/isCertif',
                    data: {
                        phone: tel, //新手机号
                        oldpPone: oldphone,
                        code: message,
                        name: name,
                    },
                    headers: headers,
                    dataType: "json",
                    success: function(res) {
                        if (Number(res.errcode) == 0) {
                            layer.open({
                                content: '认证成功',
                                skin: 'msg',
                                time: 1 //1秒后自动关闭
                            });
                            $('.submitbut').html('认证成功');
                        } else {
                            $(this).removeClass('submitbutactive')
                            layer.open({
                                content: res.msg,
                                skin: 'msg',
                                time: 1 //1秒后自动关闭
                            });
                        }
                    }
                })
            }
        }
    })
})();